/**
* @description: 待办工单 -》 通信设备巡视 -》 已巡视详情
**/

<template>
  <div class="equipment-detail-wrap">
    <nav-bar class="header" back title="设备详情" @goBack="goBack"/>
    
    <!-- 设备信息 -->
    <div class="equipment-info" v-if="taskDetail.originData">
      <plateTitle title="基本信息" class="plate-title-1"/>
      <view class="equipment-info-container">
        <van-cell-group>
          <van-cell v-for="(item,index) in taskDetail.equipmentDetailArr" :key="index" :title="item.title" :value="item.text" />
        </van-cell-group>
      </view>
      
      
      <plateTitle title="巡视信息" :isRequired="false" class="plate-title-1"/>
      
      <!-- 巡视照片 -->
      <view class="patrol-info-container" v-if="taskDetail.originData.serviceState === 4">
        <view>巡视照片</view>
        <van-divider dashed />
        <div v-if="files.filter(item => item.img).length === 0" style="text-align: center;">暂无数据</div>
        <div v-else class="image-wrap">
          <div class="image-list">
            <div
              class="upload-button"
              v-for="(imgItem, index) in files"
              v-if="imgItem.img"
              :style="{'background-image': `url(${imgItem.bgImg})`}"
            >
              <img v-if="imgItem.img" :src="imgItem.img" alt="" @click.stop="imageHandle(imgItem, index, 'PREVIEW')">
            </div>
          </div>
        </div>
      </view>
      
      <!-- 备注 -->
      <view class="remark-info-container" v-if="taskDetail.originData.serviceState === 4">
        <view>备注</view>
        <van-divider dashed />
        <div class="remark-wrap">
          <textarea class="remark-text" disabled :value="taskDetail.originData.remark">
          </textarea>
        </div>
        
      </view>
      
      
      <!-- 巡视结果 -->
      <view class="result-info-container">
        <van-cell-group>
          <van-cell v-for="(item,index) in taskDetail.tourDetailArr" :key="index" :title="item.title" :value="item.text" :class="item.color"/>
        </van-cell-group>
      </view>
      <van-button block v-if="isEditable" @click="reEdit">重新编辑</van-button>

      <van-popup class="van-preview-wrap" v-model="showPreview" @click="showPreview = false">
        <van-swipe>
          <div v-for="(image, index) in files" :key="index">
            <van-swipe-item v-if="image.img" style="text-align: center;">
              <div style="width: 100%; text-align: center;">{{ image.previewName }}</div>
              <img :src="image.img" />
            </van-swipe-item>
          </div>
        </van-swipe>
      </van-popup>
    
    </div>
  
  </div>
</template>

<script>
// 设备条目对应
let map_equipment = {
  "name": "设备名称",   //设备名称
  "sitename": "所属站点",   //站点名称
  "parRoom": "所属机房",        //所属机房
  "parRack": "所属机柜编号",         //所属机柜编号
  "parRackScreen": "所属机柜屏眉",  //所属机柜屏眉
  "techType": "设备类型",  //设备类型
  "equlevel": "设备归属",   //设备归属
  "sysId": "所属传输系统", //所属传输系统
  "producerId": "设备品牌",   //设备品牌
  "modelnumber": "设备型号",    //设备型号
  "serilanum": "设备顺序",   //设备顺序
  "entityid": "实物ID",       //实物ID
  "begTim": "设备投运时间",    //设备投运时间
  "runState": "运行状态"
}

let map_photos = {
  photos: "photos"
}

// 巡视条目对应
let map_tour = {
  "serviceState": "巡视结果",      //设备状态（1 一致  2  不存在  3  新增   4  更新）
  "tourOperName": "巡视人", //巡视人姓名
  "tourTimStr": "巡视时间",   //巡视时间
}

let serviceState_map = {
  1: "一致",
  2: "不存在",
  3: "新增",
  4: "更新"
}
let serviceStateColor_map = {
  1: "blue",
  2: "red",
  3: "green",
  4: "yellow"
}

let fileTemplate = [
  {
    name: "EQUIPMENT.jpg",
    previewName: "设备照片",
    type: "EQUIPMENT",
    bgImg: require("../../../../static/img/bg_shebei.png"),
    raw: null, // raw文件
    img: '', // 用来显示的照片
  },
  {
    name: "NAMEPLATE.jpg",
    previewName: "铭牌照片",
    type: "NAMEPLATE",
    bgImg: require("../../../../static/img/bg_mingpai.png"),
    raw: null, // raw文件
    img: '', // 用来显示的照片
  },
  {
    name: "OTHER.jpg",
    previewName: "其他照片",
    type: "OTHER",
    bgImg: require("../../../../static/img/bg_qita.png"),
    raw: null, // raw文件
    img: '', // 用来显示的照片
  },
];

import config from "../../../../common/config";
import navBar from "../../../../components/common/nav-bar";
import plateTitle from '../../../../components/business/plateTitle.vue';
export default {
  name: "equipmentDetail", // 通信设备详情
  data () {
    return {
      taskDetail: {}, // 巡视详情
      baseUrl: "", // 基础url
      objId:'',// 设备id
      taskType:'',// 设备状态
      taskSiteName:'',
      taskSiteId:'',
      isEditable: false, // 是否可重新编辑
      files: fileTemplate,
      showPreview: false,
    }
  },
  onLoad (options) {
    this.objId = options.objId;
    this.taskType = options.state;
    this.taskSiteName = options.taskSiteName
    this.taskSiteId = options.taskSiteId
    this.getEquipmentInfo()
    config.getOptions.then(res => {
      this.baseUrl = res.firstUrl;
    })
  },
  methods: {
    goBack() {
        uni.navigateTo({
          url: `/pages/workBench/todoTask/equipmentTour/equipmentTour?taskId=${this.taskDetail.originData.taskId}&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
        })
    },
    // 获取设备详情
    getEquipmentInfo () {
      this.$api.todoTaskApi.equipemntDetail({objId: this.objId}).then(res => {
        // 如果当前项为当前操作人巡视完成的，状态为未完成，设备状态为更新或新增，则显示编辑按钮支持编辑，并且状态为未完成，则显示编辑按钮支持编辑
        this.isEditable = (
          res.data.tourOperId === this.$store.state.userData.userId &&
          this.taskType !== '99' &&
          (res.data.serviceState === 3 || res.data.serviceState === 4)
        )
        this.taskDetail = {
          originData: res.data,
          equipmentDetailArr: Object.keys(map_equipment).map(item => {
            return {
              title: map_equipment[item],
              text: res.data[item]
            }
          }),
          tourDetailArr: Object.keys(map_tour).map(item => {
            let text = res.data[item];
            let color = serviceStateColor_map[text];
            if (item === 'serviceState') {
              text = serviceState_map[text];
            }
            return {
              title: map_tour[item],
              text: text,
              color: color
            }
          }),
        };

        this.files = this.files.map((item) => {
          return {
            ...item,
            img: res.data.photoMap[item.type] ? this.baseUrl + res.data.photoMap[item.type]: '',
          }
        })

      })
    },
    imageHandle (imgItem, imgIndex, handleType) {
      if (handleType === 'CHOOSE') {
        this.choosePhoto(imgItem, imgIndex);
      } else if (handleType === 'PREVIEW') {
        this.showPreview = true;
      } else if (handleType === 'DELETE') {
        Dialog.confirm({
          message: `是否确认删除${imgItem.previewName}`
        })
          .then(() => {
            this.$set(this.files, imgIndex, {
              ...this.files[imgIndex],
              raw: null,
              img: ''
            })
            this.delList.includes(imgItem.type) ? null: this.delList.push(imgItem.type);
              console.log(this.delList, "delete")

          })
          .catch(() => {
            // on cancel
          })
      }
    },
    
    // 重新编辑
    reEdit() {
      let params = this.taskDetail.originData;
      uni.navigateTo({
        url: `/pages/workBench/todoTask/equipmentTour/completeEquipmentInfo?type=update&objId=${params.objId}&taskId=${params.taskId}&state=${this.taskType}&taskSiteId=${this.taskSiteId}&taskSiteName=${params.sitename}&reTourCheck=0`
      });
      
      // 设备状态（1 一致  2  不存在  3  新增   4  更新）
      // 判断上次巡视结果，如果为一致/不存在，跳转核查页
      // let params = this.taskDetail.originData;
      // if(params.serviceState === 1 || params.serviceState === 2) {
      //   uni.navigateTo({
      //     url: `/pages/workBench/todoTask/equipmentTour/equipmentPatrol?objId=${params.objId}&taskSiteId=${params.siteid}&state=${this.taskType}&taskId=${params.taskId}&taskSiteName=${params.sitename}&reTourCheck=0`
      //   })
      // }
      // // 如果为更新/新增，跳转完善页
      // else if(params.serviceState === 4 || params.serviceState === 3) {
      //   uni.navigateTo({
      //     url: `/pages/workBench/todoTask/equipmentTour/completeEquipmentInfo?type=update&objId=${params.objId}&taskId=${params.taskId}&state=${this.taskType}&taskSiteName=${params.sitename}&reTourCheck=0`
      //   })
      // }
    }
  },
  components: {
    navBar,
    plateTitle,
  },
}
</script>

<style lang="scss" scoped>

.equipment-detail-wrap {
  .header {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  width: 100vw;
  min-height: 100vh;
  background-color: rgba(242,246,250,1);

  .van-preview-wrap {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    .van-swipe {
      width: 100%;
      height: 60%;
    }
    img {
      height: 90%;
    }
  }
  
  .equipment-state{
    width: 100vw;
    height: px2vh(140);
    background-color: #ffffff;
    box-sizing: border-box;
    padding: 0 px2vw(30) 0 px2vw(49);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .equipment-state-title {
      font-size: px2vw(42);
    }
    .equipment-state-text {
      width: px2vw(220);
      height: px2vh(80);
      border-radius: px2vw(40);
      font-size: px2vw(42);
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      &.equipment-state-on {
        color: rgba(255,67,85,1);
        background-color: rgba(255,67,85,0.1);
      }
      
      &.equipment-state-off {
        color: rgba(65,220,131,1);
        background-color: rgba(65,220,131,0.1);
      }
    }
  }
  
  
  .equipment-info {
    width: calc(100% - #{px2vw(60)});
    margin: auto;
    padding-bottom: px2vh(30);
    .plate-title-1 {
      margin-top: px2vh(30);
      margin-bottom: px2vh(27);
    }
    .equipment-info-container, .result-info-container {
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
    }
  
    .result-info-container {
      .yellow {
        /deep/.van-cell__value {
          color: #FFAD0E;
        }
      }
      .blue {
        /deep/.van-cell__value {
          color: #417ADC;
        }
      }
      .green {
        /deep/.van-cell__value {
          color: #41DC83;
        }
      }
      .red {
        /deep/ .van-cell__value {
          color: #FF4355;
        }
      }
    }
    
    .remark-info-container {
      padding: px2vh(70) px2vw(50);
      font-size: px2vw(42);
      color: #666666;
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
      margin-top: px2vh(40);
      margin-bottom: px2vh(40);
      .remark-wrap {
        background-color: #FAFAFA;
        padding: px2vh(41) px2vw(56) px2vh(77) px2vw(55);
        border-radius: px2vw(20);
        .remark-text {
          height: px2vh(150);
        }
      }
    }
    
    .patrol-info-container {
      padding: px2vh(70) px2vw(50);
      font-size: px2vw(42);
      color: #666666;
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
      
      /deep/.van-radio {
        margin-right: px2vw(139.5)
      }
      
      .image-wrap {
        width: 100%;
        display: flex;
        img {
          width: px2vw(215);
          height: px2vw(215);
          margin-right: px2vw(40);
          border-radius: px2vw(6);
        }
        
        .image-list {
          width: 100%;
          display: flex;
          .upload-button {
            width: px2vw(215);
            height: px2vw(215);
            background-size: cover;
            margin-right: px2vw(40);
            position: relative;
            .delete-icon {
              position: absolute;
              top: px2vw(-20);
              right: px2vw(-20);
              width: px2vw(40);
              height: px2vw(40);
              background-color: rgba(0, 0, 0, 0.7);
              border-radius: 50%;
              color: #fff;
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
        }
      }
    }
  }
  
  .fibre-info {
    .fibre-rate {
      width: calc(100% - #{px2vw(60)});
      height: px2vh(140);
      border-radius: px2vw(20);
      box-sizing: border-box;
      margin: px2vh(40) px2vw(30) px2vh(60);
      background-color: #ffffff;
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .fibre-rate-title {
        font-size: px2vw(42);
        color: rgba(65,122,220,1);
        margin-left: px2vw(40);
      }
      
      .fibre-rate-chart {
        width: px2vw(660);
        height: px2vh(34);
        margin: 0 px2vw(27) 0 px2vw(30);
        background-color: #ECF1FB;
        .fibre-rate-chart-off {
          width: 75%;
          height: 100%;
          background: repeating-linear-gradient(135deg, #417ADC, #417ADC #{px2vw(16)},#B3CAF1 #{px2vw(16)}, #B3CAF1 #{px2vw(32)});
        }
      }
      
      .fibre-rate-text {
        font-size: px2vw(42);
        margin-right: px2vw(40);
        font-weight: bold;
        color: #7C7C7C;
        .fibre-rate-off {
          color: #417ADC;
        }
      }
    }
    
    
  }
  
  .fibre-content-title {
    width: calc(100% - #{px2vw(80)});
    box-sizing: border-box;
    margin: px2vh(0) px2vw(40) px2vh(18);
    .condition-bar {
      display: flex;
      justify-content: center;
      .condition-item {
        height: px2vh(56);
        background-color: rgba(65,122,220,0.1);
        border: #{px2vw(2)} solid rgba(65,122,220,1);
        color: rgba(65,122,220,1);
        border-radius: px2vw(6);
        font-size: px2vw(30);
        padding: 0 px2vw(16) 0 px2vw(19);
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 0 px2vw(5);
        &:last-child {
          margin-right: 0;
        }
        &.condition-item-checked {
          background-color: rgba(65,122,220,1);
          color: rgba(255,255,255,1);
        }
      }
    }
  }
  
  .fibre-pair-list {
    .fibre-pair-item {
      width: calc(100% - #{px2vw(60)});
      height: px2vh(140);
      border-radius: px2vw(20);
      box-sizing: border-box;
      margin: px2vh(40) px2vw(30) px2vh(40);
      background-color: #ffffff;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .replace-index {
        margin: 0 px2vw(28) 0 px2vw(40);
        width: px2vw(64);
        height: px2vw(64);
        border-radius: 50%;
        background: radial-gradient(circle at center,#417ADC, #417ADC #{px2vw(23)}, #ffffff #{px2vw(23)},#ffffff #{px2vw(26)}, #417ADC #{px2vw(26)}, #417ADC 100%);
        color: #ffffff;
        font-size: px2vw(36);
        font-weight: bold;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      
      .replace-text {
        font-size: px2vw(42);
        color: #666666;
      }
      
      .replace-state {
        font-size: px2vw(42);
        margin-right: px2vw(39);
        
        &.replace-state-on {
          color: #41DC83;
        }
        
        &.replace-state-off {
          color: #FF4355;
        }
      }
      
      
    }
  }
  /deep/.van-button {
    margin-top: px2vh(30);
    height: px2vh(120);
    color: #fff;
    background-color: #417adc;
    border: 1px solid #417adc;
    border-radius: px2vh(60);
  }
}
</style>